package com.yxx.business.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import com.yxx.common.annotation.RepeatSubmit;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import com.yxx.common.annotation.Log;
import com.yxx.common.core.controller.BaseControllerPlus;
import com.yxx.common.enums.BusinessType;
import com.yxx.business.entity.TbEquipmentInfoDo;
import com.yxx.business.entity.TbEquipmentInfoVo;
import com.yxx.business.entity.TbEquipmentInfoQueryDto;
import com.yxx.business.entity.TbEquipmentInfoEditDto;
import com.yxx.business.service.ITbEquipmentInfoService;
import com.yxx.common.utils.poi.ExcelUtil;
import com.yxx.common.core.domain.R;
import com.yxx.common.core.domain.PageResult;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springdoc.api.annotations.ParameterObject;

import org.springframework.validation.annotation.Validated;


/**
 * 设备信息Controller
 *
 * @author yxx
 * @date 2025-08-11
 */
@Tag(name = "设备信息管理-TbEquipmentInfo")
@RestController
@RequestMapping("/business/equipmentInfo")
@RequiredArgsConstructor(onConstructor_ = {@Lazy, @Autowired})
public class TbEquipmentInfoController extends BaseControllerPlus {

    private final ITbEquipmentInfoService tbEquipmentInfoService;

    @Operation(summary = "查询--设备信息列表")
    @GetMapping("/list")
    public PageResult<TbEquipmentInfoVo> list(@ParameterObject TbEquipmentInfoQueryDto tbEquipmentInfoQueryDto) {
        return tbEquipmentInfoService.selectTbEquipmentInfoVoPage(tbEquipmentInfoQueryDto);
    }

    @Operation(summary = "查询--设备信息单个")
    @GetMapping(value = "/{equipmentInfoId}")
    public R<TbEquipmentInfoVo> getInfo(@PathVariable Long equipmentInfoId) {
        return R.ok(tbEquipmentInfoService.selectTbEquipmentInfoVoByEquipmentInfoId(equipmentInfoId));
    }

    @Operation(summary = "新增--设备信息")
    @Log(title = "设备信息", businessType = BusinessType.INSERT)
    @PostMapping
    @RepeatSubmit(interval = 2000, message = "您操作的太频繁了，结果正在保存中，请稍后重试")
    public R<String> add(@RequestBody @Validated TbEquipmentInfoEditDto tbEquipmentInfoQueryDto) {
        return toResult(tbEquipmentInfoService.insertTbEquipmentInfo(tbEquipmentInfoQueryDto));
    }

    @Operation(summary = "修改--设备信息")
    @Log(title = "设备信息", businessType = BusinessType.UPDATE)
    @PutMapping("/{equipmentInfoId}")
    public R<String> update(@PathVariable Long equipmentInfoId, @RequestBody @Validated TbEquipmentInfoEditDto tbEquipmentInfoQueryDto) {
        return toResult(tbEquipmentInfoService.updateTbEquipmentInfo(equipmentInfoId, tbEquipmentInfoQueryDto));
    }

    @Operation(summary = "删除--设备信息")
    @Log(title = "设备信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{equipmentInfoIds}")
    public R<String> delete(@PathVariable List<Long> equipmentInfoIds) {
        return toResult(tbEquipmentInfoService.deleteTbEquipmentInfoByEquipmentInfoIds(equipmentInfoIds));
    }

    @Operation(summary = "导出--设备信息列表")
    @PreAuthorize("@ss.hasPermi('business:equipmentInfo:export')")
    @Log(title = "设备信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, @ParameterObject TbEquipmentInfoQueryDto tbEquipmentInfoQueryDto) {
        List<TbEquipmentInfoDo> list = tbEquipmentInfoService.selectTbEquipmentInfoDoList(tbEquipmentInfoQueryDto);
        ExcelUtil<TbEquipmentInfoDo> util = new ExcelUtil<>(TbEquipmentInfoDo.class);
        util.exportExcel(response, list, "设备信息数据");
    }

    @Operation(summary = "导出--设备信息模板")
    @PreAuthorize("@ss.hasPermi('business:equipmentInfo:import')")
    @PostMapping("/importTemplate")
    public void exportTemplate(HttpServletResponse response) {
        ExcelUtil<TbEquipmentInfoDo> util = new ExcelUtil<>(TbEquipmentInfoDo.class);
        util.importTemplateExcel(response, "导入设备信息模板数据");
    }

    @Operation(summary = "导入--设备信息列表")
    @PreAuthorize("@ss.hasPermi('business:equipmentInfo:import')")
    @Log(title = "设备信息", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public R<Boolean> importData(MultipartFile file) throws IOException {
        ExcelUtil<TbEquipmentInfoDo> util = new ExcelUtil<>(TbEquipmentInfoDo.class);
        List<TbEquipmentInfoDo> list = util.importExcel(file.getInputStream());
        return R.ok(tbEquipmentInfoService.saveBatch(list), "成功导入 " + list.size() + " 条记录");
    }

    @Operation(summary = "新增--批量添加设备信息")
    @Log(title = "设备信息", businessType = BusinessType.INSERT)
    @PostMapping("/import")
    @RepeatSubmit(interval = 2000, message = "您操作的太频繁了，结果正在保存中，请稍后重试")
    public R<Boolean> importEquipmentList(@RequestBody List<TbEquipmentInfoEditDto> list) {
        return R.ok(tbEquipmentInfoService.importEquipmentList(list));
    }
}
